package pl.register.android.activities.profile;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.inject.Inject;
import pl.register.android.R;
import pl.register.android.activities.gallery.StudentGalleryActivity;
import pl.register.android.activities.profile.logic.AsyncLessonTablesLoader;
import pl.register.android.activities.profile.logic.DeleteStudentConfirmationDialogBuilder;
import pl.register.android.activities.profile.logic.StudentEditorDialogBuilder;
import pl.register.android.activities.profile.logic.StudentProfilePresenter;
import pl.register.android.activities.profile.ui.StudentProfileView;
import pl.register.android.database.DatabaseAdapter;
import pl.register.android.roboguice.activity.RoboSherlockActivity;
import pl.register.android.student.Student;
import pl.register.android.util.StringUtils;
import roboguice.inject.ContentView;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-20
 */

@ContentView(R.layout.student_profile_layout)
public class StudentProfileActivity extends RoboSherlockActivity implements StudentProfileView {

    public static final String EXTRA_STUDENT_ID = "student_id_key";

    public static final String EXTRA_YEAR_ID = "student_year_id_key";

    public static final String EXTRA_COURSE_ID = "student_course_id_key";

    @InjectView(R.id.student_phone_number_text_view)
    private TextView tvPhoneNo;

    @InjectView(R.id.student_city_text_view)
    private TextView tvCity;

    @InjectView(R.id.student_class_text_view)
    private TextView tvClass;

    @InjectView(R.id.student_email_text_view)
    private TextView tvEmail;


    @InjectView(R.id.classes_student_table)
    private LinearLayout classesTableContainer;

    @InjectView(R.id.homeworks_student_table)
    private LinearLayout homeworksTableContainer;

    @Inject
    private StudentProfilePresenter presenter;

    private DatabaseAdapter databaseAdapter;

    @InjectExtra(EXTRA_YEAR_ID)
    private Long yearId;

    @InjectExtra(EXTRA_COURSE_ID)
    private Long courseId;

    @InjectExtra(EXTRA_STUDENT_ID)
    private Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        databaseAdapter = new DatabaseAdapter(this);
        databaseAdapter.open();
        presenter.init(student, databaseAdapter, this);

        new AsyncLessonTablesLoader(this, this, presenter, courseId, yearId).execute();

        onCreateActionBar();
        actualizeStudentDetails(student);
    }

    private void onCreateActionBar() {
        getSupportActionBar().setIcon(R.drawable.ic_user_holo_dark);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void actualizeStudentDetails(Student student) {
        String studentName = StringUtils.build("     ", student.getFirstName(), " ", student.getLastName());
        getSupportActionBar().setTitle(studentName);
        tvPhoneNo.setText(student.getPhoneNo());
        tvCity.setText(student.getCity());
        tvClass.setText(student.getClassNo());
        tvEmail.setText(student.getEmailAddress());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.menu_student_profile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_student_gallery:
                Intent intent = new Intent(this, StudentGalleryActivity.class);
                intent.putExtra(StudentGalleryActivity.EXTRA_STUDENT_ID, student);
                startActivity(intent);
                return true;
            case R.id.menu_student_editor:
                AlertDialog alertDialogEditor = new StudentEditorDialogBuilder(this, student, presenter).create();
                alertDialogEditor.show();
                alertDialogEditor.getWindow().setLayout(700, 600);
                return true;
            case R.id.menu_student_delete:
                AlertDialog alertDialog = new DeleteStudentConfirmationDialogBuilder(this, student, presenter).create();
                alertDialog.show();
                alertDialog.getWindow().setLayout(600, 400);
                return true;
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void settleClassesTable(View table) {
        classesTableContainer.removeAllViews();
        classesTableContainer.addView(table);
    }

    @Override
    public void settleHomeworksTable(View table) {
        homeworksTableContainer.removeAllViews();
        homeworksTableContainer.addView(table);
    }

    @Override
    public void showToastNotFoundGroup() {
        Toast.makeText(this, "Kursant nie został przydzielony no żadnej z grup.", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (databaseAdapter != null) {
            databaseAdapter.close();
        }
    }

    @Override
    public void finishActivity() {
        finish();
    }

}